package Hot100.Tree;

import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {
    TreeNode root;

    public BinaryTree() {
        this.root = null;
    }

    public void insert(int[] values) {
        if (values == null || values.length == 0) {
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        int i = 0;

        root = new TreeNode(values[i++]);
        queue.offer(root);

        while (!queue.isEmpty()) {
            TreeNode current = queue.poll();

            if (i < values.length) {
                int leftVal = values[i++];
                if (leftVal != -1) {
                    current.left = new TreeNode(leftVal);
                    queue.offer(current.left);
                }
            }

            if (i < values.length) {
                int rightVal = values[i++];
                if (rightVal != -1) {
                    current.right = new TreeNode(rightVal);
                    queue.offer(current.right);
                }
            }
        }
    }
}
